package collection.test.chatroom;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

/**
 * 服务端
 */
public class Server {
    private ServerSocket serverSocket;
    private PrintWriter[] messages = {};//信息写出数组
    public Server(){
        try {
            serverSocket = new ServerSocket(8088);
            System.out.println("服务端已开启");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start(){
        try {
            while (true){
            System.out.println("等待客户连接...");
            Socket socket = serverSocket.accept();
            Runnable client = new multiClient(socket);
            Thread thread = new Thread(client);
            thread.start();
            System.out.println("客户已连接..");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
           /* try {
                serverSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }*/
        }
    }
    class multiClient implements Runnable{
        private Socket socket;
        private String host;
        public multiClient(Socket socket){
            this.socket = socket;
            this.host = socket.getInetAddress().getHostAddress();
        }
        @Override
        public void run() {
            PrintWriter pw = null;
            try {
                //接收客户端数据
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);

                //返回客户端数据
                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os, StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw);
                pw = new PrintWriter(bw,true);

                synchronized (Server.this){
                    //将字符输出流存入数组
                    messages = Arrays.copyOf(messages,messages.length+1);
                    messages[messages.length - 1]=pw;
                }

                sendMessage(host+"上线了,共有"+messages.length+"人在线");

                String line;
                while ((line = br.readLine())!= null){
                    sendMessage(host+"说:"+line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                //当客户端断开后,数组缩容
                for (int i = 0; i < messages.length; i++) {
                    if (pw == messages[i]){
                        synchronized (Server.this){
                            messages[i] = messages[messages.length - 1];
                            messages = Arrays.copyOf(messages,messages.length-1);
                        }
                        break;
                    }
                }
                sendMessage(host+"已下线,当前在线人数:"+messages.length);
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        public void sendMessage(String line){
            System.out.println(line);//服务端信息输出
            for (int i = 0; i < messages.length; i++) {
                messages[i].println(line);
            }
        }
    }
    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }
}
